package com.pclong.doudizhu.server;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import java.net.*;
import java.io.*;
import java.util.*;

/**
 *
 * @author long@7road.com
 * @Date 2012-2-29,10:21:14
 * @Description ServerChat类
 */
public class ServerChat {

    private static final Logger log = LogManager.getLogger(ServerChat.class);
    private ServerSocket server;
    private BManager bMan = new BManager();//消息广播者

    public ServerChat() {
    }  //构造函数

    void startServer() //启动服务器
    {
        try {
            server = new ServerSocket(8885); //创建服务器套接字
            System.out.println("服务器套接字建立完毕");
            while (true) {
                Socket socket = server.accept();//若客户机提请求，socket连接                
                new Chat_Thread(socket).start();//启动线程
                bMan.add(socket);//添加套接字
                bMan.sendClientInfo();//使用套接字输出当前聊天人数
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public static void main(String[] args) {
        ServerChat server = new ServerChat();
        server.startServer();
    }

    class Chat_Thread extends Thread //与客户机进行通信的线程累
    {

        Socket socket; //套接字引用变量
        private BufferedReader reader;//引用套接字输入流；
        private PrintWriter writer;//引用套接字输出流；

        Chat_Thread(Socket socket)//构造函数
        {
            this.socket = socket;
        }

        public void run() {                     
            try {
                 while (true) {                
                  reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "utf8"));
                writer = new PrintWriter(socket.getOutputStream(), true);
                String msg;
                //从输出流获取信息
                while ((msg = reader.readLine()) != null) {
                    System.out.println(msg);
                    //向所有客户机传送信息
                    //bMan.sendToAll(msg);
                    bMan.sendToAll("服务器消息："+msg);
                }
            }        
           } catch (Exception e) {
            } finally {}
           /*
              reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "utf8"));
                writer = new PrintWriter(socket.getOutputStream(), true);
                String msg;
                //从输出流获取信息
                while ((msg = reader.readLine()) != null) {
                    System.out.println(msg);
                    //向所有客户机传送信息
                    //bMan.sendToAll(msg);
                }
            } catch (Exception e) {
            } finally {
                try {//从消息广播者立标中删除socket
                    bMan.remove(socket);
                    if (reader != null) {
                        reader.close();
                    }
                    if (writer != null) {
                        writer.close();
                    }
                    if (socket != null) {
                        socket.close();
                    }
                    reader = null;
                    writer = null;
                    socket = null;
                    System.out.println("客户机离开");
                    //向所有客户机传送当前连接数
                    bMan.sendClientInfo();
                } catch (Exception e) {
                }
            }
*/
        }
    }
}

//消息广播者类，继承Vector类
class BManager extends Vector {

    BManager() {
    }//构造函数

    void add(Socket sock) {
        super.add(sock);//添加套接字
    }

    void remove(Socket sock) {
        super.remove(sock);//删除套接字
    }
    //向所有客户机传送消息，同步化方法。

    synchronized void sendToAll(String msg) {
        PrintWriter writer = null; //输出流
        Socket sock;  //套接字
        for (int i = 0; i < size(); i++) //执行循环
        {
            sock = (Socket) elementAt(i);//获取第i个套接字
            try {        //获取第i个套接字输出流
                writer = new PrintWriter(sock.getOutputStream(), true);
            } catch (Exception ie) {
            }
//使用第i各套接字输出流，输出消息
            if (writer != null) {
                writer.println(msg);
            }
        }
    }

    //向所有客户机发送当前聊天人数
    synchronized void sendClientInfo() {
        String info = "当前聊天人数：" + size();
        System.out.println(info);
        sendToAll(info);
    }
}



